<?php

namespace addons\shopro\service;

use app\common\model\FirepowerLog;
use app\common\model\GoldenLog;
use app\common\model\GoldLog;
use app\common\model\PrincipalLog;
use app\common\model\SilverLog;
use app\common\model\UserStaticLogModel;
use think\exception\HttpResponseException;
use app\admin\model\shopro\user\User as UserModel;
use app\admin\model\shopro\user\WalletLog;
use addons\shopro\library\Operator;
use app\common\model\MoneyLog;
use app\common\model\ScoreLog;

class Wallet
{
    /**
     * @name 变更会员资金
     * @param  int|object  $user  会员对象或会员ID
     * @param  string  $type  变更类型:money=代金券,commission=佣金,score=积分，gold=金币，golden=黄金券，silver=银豆,firepower=火力值
     * @param  float|string  $amount  变更数量
     * @param  array  $ext  扩展字段
     * @param  string  $memo  备注
     * @return boolean
     */
    public static function change($user, $type, $amount, $event, $ext = [], $memo = '')
    {
        // 判断用户
        if (is_numeric($user)) {
            $user = UserModel::getById($user);
        }
        if (!$user) {
            error_stop('未找到用户');
        }
        // 判断金额
        if ($amount == 0) {
            error_stop('请输入正确的金额');
        }
        if (!in_array($type, ['money', 'score', 'commission', 'gold', 'golden', 'silver','firepower','principal'])) {
            error_stop('参数错误');
        }

        $before = $user->$type;
        $after  = bcadd((string)$user->$type, (string)$amount, 2);
        // 只有后台扣除用户代金券、扣除用户积分、佣金退回，钱包才可以是负值
        if ($after < 0 && !in_array($event, ['admin_recharge', 'reward_back'])) {
            $walletTypeText = WalletLog::TYPE_MAP[$type];
            error_stop("可用{$walletTypeText}不足");
        }
        try {
            // 更新会员代金券信息
           $r= $user->setInc($type, $amount);
//           if($type == 'score' || $type == 'principal'){
//                if($event == 'draw' || $event == 'buy_financial'){
//                    if($type == 'score'){
//                        $user->setInc('total_consume',abs($amount));
//                    }else{
//                        $user->setInc('principal_total_consume',abs($amount));
//                    }
//                }
//           }
//            if($amount < 0 && $event != 'admin_recharge' && $type == 'score' || $type == 'principal'){
//                $total_consume = abs($amount);
//                $user->setInc('total_consume',$total_consume);
//            }
            // 获取操作人
            $oper = Operator::get();
            // 写入日志
            $walletLog = WalletLog::create([
                'user_id'   => $user->id,
                'amount'    => $amount,
                'type'      => $type,
                'before'    => $before,
                'after'     => $after,
                'event'     => $event,
                'memo'      => $memo,
                'ext'       => $ext,
                'oper_type' => $oper['type'],
                'oper_id'   => $oper['id']
            ]);

            // 钱包和积分记录存到 fastadmin 钱包积分记录表
            if (in_array($type, ['money', 'score','gold', 'golden', 'silver','firepower','principal'])) {
                $eventMap = (new WalletLog)->getEventMap();

                $memo     = $memo ?: $eventMap[$type][$event] ?? '';
                if ($type === 'money') {
                    MoneyLog::create([
                        'user_id' => $user->id,
                        'money'   => $amount,
                        'before'  => $before,
                        'after'   => $after,
                        'memo'    => $memo
                    ]);
                } else if ($type === 'score'){
                    ScoreLog::create([
                        'user_id' => $user->id,
                        'score'   => $amount,
                        'before'  => $before,
                        'after'   => $after,
                        'memo'    => $memo
                    ]);
                }else if ($type === 'principal'){
                    PrincipalLog::create([
                        'user_id' => $user->id,
                        'principal'   => $amount,
                        'before'  => $before,
                        'after'   => $after,
                        'memo'    => $memo
                    ]);
                } else if ($type === 'gold'){
                    GoldLog::create([
                        'user_id' => $user->id,
                        'gold'   => $amount,
                        'before'  => $before,
                        'after'   => $after,
                        'memo'    => $memo
                    ]);
                }else if ($type === 'golden'){
                    GoldenLog::create([
                        'user_id' => $user->id,
                        'golden'   => $amount,
                        'before'  => $before,
                        'after'   => $after,
                        'memo'    => $memo
                    ]);
                }else if ($type === 'silver'){
                    SilverLog::create([
                        'user_id' => $user->id,
                        'silver'   => $amount,
                        'before'  => $before,
                        'after'   => $after,
                        'memo'    => $memo
                    ]);
                }else if ($type === 'firepower'){
                    FirepowerLog::create([
                        'user_id' => $user->id,
                        'firepower'   => $amount,
                        'before'  => $before,
                        'after'   => $after,
                        'memo'    => $memo
                    ]);
                }
            }
            // 账户变动事件
//            $data = ['walletLog' => $walletLog, 'type' => $type];
//            \think\Hook::listen('user_wallet_change', $data);
        } catch (HttpResponseException $e) {
            $data    = $e->getResponse()->getData();
            $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
            error_stop($message);
        } catch (\Exception $e) {
            error_stop($e->getMessage());
        }
        return true;
    }
}
